package cn.tedu.straw.portal.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {

    @Autowired
    UserDetailsServiceImpl userDetailsService;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception{
        auth.userDetailsService(userDetailsService);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception{

        // 登录页面
        String loginPageUrl = "/login.html";
        // 处理登录的URL，即登录表单提交到哪里去
        String loginProcessingUrl = "/login";
        //不需要登录就可以访问
        String[] urls={
                loginPageUrl,
                loginProcessingUrl,
                "test/user/info",
                "/browser_components/**",
                "/css/**",
                "/img/**",
                "/js/**",
                "/npm/**",
                "/register.html",
                "/portal/user/reg/student"
        };
        //授权访问
        http.authorizeRequests()
                .antMatchers(urls).permitAll()
                .antMatchers("/test/user/add").hasAnyAuthority("创建用户","系统管理员")
                .antMatchers("/test/user/delete").hasAnyAuthority("删除用户","系统管理员")
                .antMatchers("/test/user/update").hasAnyAuthority("修改用户","系统管理员")
                .antMatchers("/test/user/select").hasAnyAuthority("查看用户","系统管理员")
                .anyRequest().authenticated();


                http.formLogin()
                        .loginPage(loginPageUrl)
                        .loginProcessingUrl(loginProcessingUrl);

                http.csrf().disable();


    }
}
